package com.datastructures.linkedList;

/**
 * @author MaoLin Wang
 * @date 2019/10/1911:37
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {

        //先创建几个节点
        Person2 person1 = new Person2(1, "datastructures", "小m");
        Person2 person2 = new Person2(2, "wml2", "小m2");
        Person2 person3 = new Person2(3, "wml3", "小m3");
        Person2 person4 = new Person2(4, "wml4", "小m4");
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add2(person1);
        doubleLinkedList.add2(person4);
        doubleLinkedList.add2(person2);
        doubleLinkedList.add2(person3);
        doubleLinkedList.printData();
        System.out.println("=======");
/*
        System.out.println("-------修改后--------");
        //修改
        Person2 newP = new Person2(2, "wml修改", "2");
        doubleLinkedList.update(newP);
        doubleLinkedList.printData();
        System.out.println("----删除-------");
        doubleLinkedList.delete(2);*/
        Person2 person5 = new Person2(5, "wml5", "小m5");
        Person2 person6 = new Person2(6, "wml5", "小m5");

        doubleLinkedList.addTail(person5);
        doubleLinkedList.addAfter(person5,person6);
        doubleLinkedList.deleteTarget(person6);
        doubleLinkedList.printData();
    }
}

//双向链表类
class DoubleLinkedList {

    private Person2 head = new Person2(0, "", "");

    //返回头节点
    public Person2 getHead() {
        return head;
    }

    public void deleteTarget(Person2 person2){
        if (head.next==null){
            return;
        }
        if (person2==null){
            return;
        }
        Person2 temp=head.next;
        if (temp==person2){
            deleteHead();
            return;
        }
        temp=temp.next;
        while (temp!=person2){
            if (temp.next==null){
                System.out.println("没找到目标节点");
                return;
            }
            temp=temp.next;
        }
        if (temp.next==null){
            deleteTail();
            return;
        }
        temp.pre.next=temp.next;
        temp.next.pre=temp.pre;
        temp.next=null;
        temp.pre=null;

    }
    /**
     * 在指定节点后插入节点
     * @param target
     * @param newNode
     */
    public void addAfter(Person2 target,Person2 newNode) {
        if (target==null||newNode==null){
            return;
        }
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        Person2 temp=head.next;
        while (temp!=target){
            if (temp.next==null){
                System.out.println("未找到目标节点");
                return;
            }
            temp=temp.next;
        }
        newNode.pre=temp;
        if (temp.next==null){
            temp.next=newNode;
        }else {
            newNode.next=temp.next;
            target.next.pre=newNode;
            target.next=newNode;
        }

    }
        /**
         * 在指定节点前插入节点
         * @param target 指定节点
         * @param newNode 新节点
         */
    public void addBefore(Person2 target,Person2 newNode){
        if (target==null){
            System.out.println("节点为空");
            return;
        }
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        Person2 temp=head.next;
        boolean flag=true;
        while (temp.next!=target){
            if (temp.next==null){
                flag=false;
                break;
            }
            temp=temp.next;
        }
        if (!flag){
            System.out.println("不存在目标节点");
            return;

        }
        newNode.next=target;
        newNode.pre=temp;
        temp.next.pre=newNode;
        temp.next=newNode;

    }

    public void deleteTail(){
        if (head.next==null){
            return;
        }
        Person2 temp=head.next;
        while (temp.next!=null){
            temp=temp.next;
        }
        temp.pre.next=null;
        temp.pre=null;
    }
    public void deleteHead(){
        if (head.next==null){
            return;
        }
        head.next=head.next.next;
        head.next.next.pre=head;
    }
    public void addTail(Person2 person2){
        if (getHead().next==null){
            head.next=person2;
            person2.pre=head;
            return;
        }
        Person2 temp=head.next;
        while (temp.next!=null){
            temp=temp.next;
        }
        temp.next=person2;
        person2.pre=temp;

    }
    public void addHead(Person2 person2){
        if (getHead().next==null){
            head.next=person2;
            person2.pre=head;
            return;
        }
        person2.next=head.next;
        person2.pre=head;
        head.next.pre=person2;
        head.next=person2;
    }
    /**
     * 添加节点到双向链表尾部
     * @param person
     */
    public void add(Person2 person) {
        //head节点不能动，因此需要一个辅助遍历temp
        Person2 temp = head;
        //遍历链表，找到最后
        while (true) {
            if (temp.next == null) {
                break;
            }
            //没找到，就将temp后移
            temp = temp.next;
        }
        //temp已指向最后，添加新的节点
        temp.next = person;
        person.pre = temp;
    }

    /**
     * 修改，和单项链表一样
     */
    public void update(Person2 newPerson) {

        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //找到需要修改的节点
        Person2 temp = head.next;
        boolean flag = false;//是否找到该节点
        while (true) {
            if (temp == null) {
                break;//遍历完链表
            }
            if (temp.no == newPerson.no) {
                //找到了
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (!flag) {
            System.out.println("未找到该节点");
        }
        temp.name = newPerson.name;
        temp.nickname = newPerson.nickname;
    }

    /**
     * 删除一个节点
     * 1.双向链表可直接找到要删除的这个节点
     * 2.找到后直接删除
     */
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Person2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.pre.next =temp.next;
            if(temp.next!=null){
                temp.next.pre=temp.pre;
            }

        } else {
            System.out.println("节点不存在:" + no);
        }
    }
    /**
     * 根据no将数据插入到指定位置
     */
    public void add2(Person2 person) {
        //temp是位于添加位置的前一个节点，否则插入不了
        Person2 temp = head;
        boolean flag = false;//标识添加的编号是否存在
        while (true) {
            if (temp.next == null) {
                //temp在链表最后
                break;
            }
            if (temp.next.no > person.no) {//位置找到，应该加在temp和temp.next中间
                break;
            } else if (temp.next.no == person.no) {
                //已经存在
                flag = true;
            }
            temp = temp.next;

        }
        if (flag) {
            System.out.println("已经存在，不可添加");
        }
        //添加节点
        person.pre=temp;
        if(temp.next!=null){
            temp.next.pre=person;
            person.next=temp.next;
        }
        temp.next=person;

    }
    /**
     * 打印链表
     */
    public void printData() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Person2 temp = head.next;
        while (true) {
            //是否到最后了
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
}


class Person2 {
    public int no;
    public String name;
    public String nickname;
    public Person2 next;//指向下一个节点 默认null
    public Person2 pre;//指向前一个节点 默认null

    public Person2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "Person2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
